package sample.config;

import javax.servlet.Filter;

import brave.Tracing;
import brave.dubbo.rpc.TracingFilter;
import brave.rpc.RpcTracing;
import zipkin2.Span;
import zipkin2.reporter.AsyncReporter;
import zipkin2.reporter.Sender;
import zipkin2.reporter.okhttp3.OkHttpSender;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Created on 2020/6/29
 */
@Configuration
public class TraceConfiguration {

	/**
     * Configuration for how to send spans to Zipkin
     */
	@Bean
	public Sender sender() { // Sender 采用 HTTP 通信方式
		return OkHttpSender.create("http://127.0.0.1:9411/api/v2/spans");
	}

	/**
	 * Configuration for how to buffer spans into messages for Zipkin
	 */
	@Bean
	public AsyncReporter<Span> spanReporter() { // 异步 Reporter
		return AsyncReporter.create(sender());
	}

	/**
	 * Controls aspects of tracing such as the service name that shows up in the UI
	 */
	@Bean
	public Tracing tracing(@Value("${spring.application.name}") String serviceName) {
		return Tracing.newBuilder()
				.localServiceName(serviceName) // 应用名
				.spanReporter(this.spanReporter()).build();
//        .build();
	}

	@Bean
	public RpcTracing rpcTracing(Tracing tracing) {
		return RpcTracing.create(tracing);
	}

}
